<template>
  <div v-loading.fullscreen.lock="fullscreenLoading" class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="仓库名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入仓库名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增顶级仓库</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      border
      :default-expand-all="false"
    >
      <el-table-column prop="name" label="库位名称" align="center" />
      <el-table-column label="创建时间" align="center" prop="created_at" />
      <el-table-column label="库位地址" align="center" prop="address_description" />
      <el-table-column label="联系人姓名" align="center" prop="realation_name" />
      <el-table-column label="联系人电话" align="center" prop="relation_phone" />
      <el-table-column label="查看库位" align="center" width="200">
        <template v-slot="scope">
          <span v-show="scope.row.level == 2" style="color: #20a0ff;cursor:pointer" @click="see(scope.row)">查看</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            v-if="scope.row.level === 1"
            size="mini"
            type="danger"
            plain
            @click="handleAdd(scope.row,[scope.row.id])"
          >添加库区
          </el-button>

          <template v-else-if="scope.row.level === 2">

            <el-button
              size="mini"
              plain
              type="warning"
              @click="warehouseConfigImport(scope.row,[scope.row.parent_id,scope.row.id])"
            >库位导入
            </el-button>

            <el-button
              size="mini"
              plain
              type="primary"
              @click="handleAdd(scope.row,[scope.row.parent_id,scope.row.id])"
            >添加库位
            </el-button>

          </template>

          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDel(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :add-config-level="addConfigLevel" :title="title" :form="form" :select-data="selectData" @getList="getList" />
    <!-- 库位 -->
    <Seat :opens.sync="opens" :seat="seat" :titles="titles" :total_config_counts="total_config_counts" :total_counts="total_counts" :total_layup_nums="total_layup_nums" :usage_1_counts="usage_1_counts" :vacancy_rate="vacancy_rate" />

    <WarehouseConfigImport :confid-id="importConfigId" :dialog-title="importDialogTitle" :import-open.sync="importOpen" />

  </div>
</template>
<script>
import { locationList, locationDel, seatList, att } from '@/api/stock'
import AddEdit from './AddEdit'
import Seat from './Seat'
import WarehouseConfigImport from '@/views/stock/location/WarehouseConfigImport'
export default {
  name: 'Index',
  components: {
    WarehouseConfigImport,
    AddEdit,
    Seat
  },
  data() {
    return {
      importConfigId: '',
      importOpen: false,
      importDialogTitle: '',
      addConfigLevel: 1,
      // 遮罩层
      loading: true,
      open: false,
      opens: false,
      title: '',
      titles: '',
      // 表格数据
      list: [],
      form: {},
      seat: {},
      selectData: [],
      // 查询参数
      queryParams: {
        name: undefined
      },
      show: false,
      ids: [],
      fullscreenLoading: false,
      total_config_counts: 0,
      total_counts: 0,
      total_layup_nums: 0,
      usage_1_counts: 0,
      vacancy_rate: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    warehouseConfigImport(scope = {}, form_id = [0]) {
      this.importOpen = true
      this.importConfigId = scope.id
      this.importDialogTitle = `【${scope.name}】库区`
    },
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      // console.log(this.queryParams)
      att(this.queryParams).then(response => {
        if (response.code === 10000) {
          that.list = JSON.parse(JSON.stringify(response.data))
          that.loading = false
          that.selectData = JSON.parse(JSON.stringify(response.data))
          that.selectList(that.selectData)
        }
      })
    },
    /* 处理数据 */
    selectList(data) {
      const replaceKey = function(data) {
        data.forEach(function(row, index, arr) {
          const temp = { label: row.name, value: row.id }
          if (row.children !== undefined) {
            if (row.children.length > 0) {
              temp['children'] = row.children
            }
            arr[index] = temp
            if (arr[index].children.length > 0) {
              replaceKey(arr[index].children)
            }
          } else {
            arr[index] = temp
          }
        })
      }
      replaceKey(data)
      this.selectData.unshift({ label: '顶级仓库', value: 0 })
    },
    /** 新增按钮操作 */
    handleAdd(scope = {}, form_id = [0]) {
      this.form = { id: form_id }
      this.open = true
      this.title = '添加'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      // console.log(row)
      this.form = {
        id: row.id,
        ids: row.id,
        name: row.name,
        realation_name: row.realation_name,
        relation_phone: row.relation_phone,
        isstatus: row.isstatus,
        parent_id: row.parent_id,
        _level: row.level,
        address_description: row.address_description
      }
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const that = this
      console.log(33)
      that.$confirm('是否删除该库位,如果该库位底下还有库位则一起删除,确定吗？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        locationDel({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 详情按钮操作 */
    see(row) {
      this.fullscreenLoading = true
      this.seatList(row.id, row.parent_id, row.name)
    },
    seatList(id, parent_id, name) {
      seatList({ id: id, parent_id: parent_id, name }).then(response => {
        console.log(response)
        if (response.code === 10000) {
          this.fullscreenLoading = false
          this.seat = response.data.data
          this.total_config_counts = response.data.total_config_counts
          this.total_counts = response.data.total_counts
          this.total_layup_nums = response.data.total_layup_nums
          this.usage_1_counts = response.data.usage_1_counts
          this.vacancy_rate = response.data.use_rate
          // 数据加载完毕
          this.opens = true
          this.titles = response.data.title
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        name: undefined
      }
      this.handleQuery()
    }
  }
}
</script>

